//
//  NSString+Category.m
//  GYVVBusiness
//
//  Created by LGSC on 2018/6/11.
//  Copyright © 2018年 gongyou04. All rights reserved.
//

#import "NSString+Category.h"

@implementation NSString (Category)

-(BOOL)CheckNSStringData
{
    if (self && [self isKindOfClass:[NSString class]] && self != nil && self != NULL && ![self isKindOfClass:[NSNull class]] && [[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] != 0 && ![self isEqualToString:@"null"] && ![self isEqualToString:@"(null)"]) {
        return YES;
    }
    return NO;
}

//判断字符串是否包含英文(两个字母及以上才算)
- (BOOL)isStringContainEnglish {
    
    NSRegularExpression *numberRegular = [NSRegularExpression regularExpressionWithPattern:@"[A-Za-z]" options:NSRegularExpressionCaseInsensitive error:nil];
    NSInteger count = [numberRegular numberOfMatchesInString:self options:NSMatchingReportProgress range:NSMakeRange(0, self.length)];
    //count是str中包含[A-Za-z]数字的个数，只要count>1(两个字母及以上才算)，说明str中包含英文
    if (count > 1) {
        return YES;
    }
    return NO;
    
}

//是否是纯数字
- (BOOL) CheckIsOnlyNum
{
    if (self.length == 0) {
        return NO;
    }
    NSString *regex = @"[0-9]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    if ([pred evaluateWithObject:self]) {
        return YES;
    }
    return NO;
}

//json 转字典
-(NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
{
    if (jsonString == nil) {
        return nil;
    }

    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err)
    {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

@end
